<style>
  #img{
    list-style-type: none;
    margin: 2px;
    height: 118px;
    float: left;
  }
  .divImg{
    float: left;
    margin: 2px;
    padding: 2px;
    height:108px;
    width:100px;
    border: 1px solid #dcdcdc;
    cursor: pointer;
  }
  .divImgD{
    float: left;
    margin: 2px;
    padding: 2px;
    height:108px;
    width:100px;
    border: 1px solid red;
    cursor: pointer;
  }
</style>
<header class="header navbar bg-white shadow">
  <div class="btn-group tool-button">
    <a class="btn btn-primary navbar-btn" href="/private/shop/goods/goods" data-pjax><i class="ti-angle-left"></i> 返回</a>
  </div>
  <div class="pull-right">
    <div class="btn-group tool-button">
      <button class="btn btn-primary navbar-btn" type="button" id="save"> 保存</button>
    </div>
  </div>
</header>

<div class="content-wrap">
  <div class="wrapper" style="min-height:500px;">
    <form id="addForm" role="form" class="form-horizontal parsley-form" data-parsley-validate
          action="/private/shop/goods/goods/editDo" method="post">
      <%var is_spec=false;
      if(goods.products.length>1){
        is_spec=true;
      }
      %>
      <input type="hidden" name="id" id="id" value="<%=goods.id%>">
      <div class="box-tab tabs-left">
        <ul class="nav nav-tabs">
          <li class="active"><a href="#base" data-toggle="tab">基本信息</a>
          </li>
          <li id="kzsx_tab" class="" style="display: <%=goods.propslist.length>0?'block':'none'%>"><a href="#kzsx" data-toggle="tab">扩展属性</a>
          </li>
          <li class=""><a href="#inf" data-toggle="tab">详细介绍</a>
          </li>
        </ul>
        <div class="tab-content text-center">
          <div class="tab-pane fade active in" id="base">
            <div class="row mb10">
              <div class="col-lg-12">
                <div class="form-group has-feedback">
                  <label for="classid" class="col-sm-2 control-label">商品分类</label>

                  <div class="col-sm-8">
                    <div class="input-group">
                      <input id="classid" type="text" class="form-control" placeholder="请选择商品分类" disabled
                             value="<%=typeof goods.classid!='undefined'?goods.classid.name:'' %>"/>

			                             		<span class="input-group-btn">
			                             			<button type="button" class="btn btn-primary" data-toggle="modal"
                                                data-target="#dialogSelectParentUnit"><i class="ti-plus"></i>选择
                                        </button>
			                             		</span>

                    </div>
                    <input type="hidden" name="classid" value="<%=typeof goods.classid!='undefined'?goods.classid.id:0 %>">
                  </div>
                </div>
                <div class="form-group">
                  <label for="typeid" class="col-sm-2 control-label">商品类型</label>

                  <div class="col-sm-8">
                    <select id="typeid" name="typeid" class="form-control">
                      <option value="0">通用商品</option>
                      <%if(typelist){
                      typelist.forEach(function(o){
                      %>
                      <option value="<%=o.id%>" <%=goods.typeid==o.id?'selected':''%>><%=o.name%></option>
                      <%
                      });
                      }%>
                    </select>
                  </div>
                </div>
                <div class="form-group">
                  <label for="name" class="col-sm-2 control-label"><span style="color: red">*</span>商品名称</label>

                  <div class="col-sm-8">
                    <input type="text" id="name" class="form-control" name="name" data-parsley-required="true"
                           placeholder="商品名称" value="<%=goods.name%>">


                  </div>
                </div>
                <!--<div class="form-group">-->
                <!--<label for="bn" class="col-sm-2 control-label">商品编号</label>-->

                <!--<div class="col-sm-8">-->
                <!--<input type="text" id="bn" class="form-control" name="bn" data-parsley-required="true"-->
                <!--placeholder="商品编号(不填则自动生成)">-->
                <!--</div>-->
                <!--</div>-->
                <div class="form-group">
                  <label for="bn" class="col-sm-2 control-label">品牌</label>

                  <div class="col-sm-8">
                    <select id="brandid" name="brandid" class="form-control">
                      <option value="0">  </option>
                      <%if(brandlist){
                      brandlist.forEach(function(o){
                      %>
                      <option value="<%=o.id%>" <%=goods.brandid==o.id?'selected':''%>><%=o.name%></option>
                      <%
                      });
                      }%>
                    </select>
                  </div>
                </div>
                <div class="form-group">
                  <label for="bn" class="col-sm-2 control-label">商品简介</label>

                  <div class="col-sm-8">
                    <textarea type="textarea" placeholder="简短的商品介绍,请不要超过70个字" class="form-control"  name="info" style="resize:none;" cols="50" rows="3" maxlength="70" ><%=goods.info||''%></textarea>
                  </div>
                </div>
                <div class="form-group">
                  <label for="bn" class="col-sm-2 control-label">商品相册</label>

                  <div class="col-sm-8">
                    <div id="queue"></div>
                    <div >
                      <input id="file_upload" name="file_upload" type="file" multiple="true">

                    </div>
                    <div id="img">
                      <%
                      var img_i=0;
                      goods.images.forEach(function(img){
                        var img_c="divImg";
                        img_i++;
                        if(goods.imgurl==img.imgurl){
                          img_c="divImgD";
                        }
                      %>
                      <div id='imgId<%=img_i%>' class='<%=img_c%>'>
                       <img  onclick="setImg('imgId<%=img_i%>')" src='<%=img.imgurl%>?type=s' style='width:100px;height: 80px;margin-bottom: 1px;'><br>
                       <i style='float: right;padding-top: 4px;' class='fa fa-close' onclick="delImg('imgId<%=img_i%>')"></i></div>
                      <%});%>
                    </div>
                  </div>
                </div>
                <div id="sp_new" style="display: <%=is_spec==true?'block':'none'%>">
                  <%if(is_spec){%>

                  <div class="form-group" id="gg_new">
                    <label for="gg_new" class="col-sm-2 control-label">规格</label>
                    <div class="col-sm-8">
                      <button id="specEditBtn" class="btn btn-primary" style="float: left;" type="button">编辑规格</button>
                      <div style="float: left;text-align: left;">已设置<%=goods.products.length%>个规格货品[<%goods.products.forEach(function(o){%><%=o.spec%><%});%>]</div></div></div>
                  <%}%>
                </div>
                <div id="sp" style="display: <%=is_spec==false?'block':'none'%>">
                  <div class="form-group">
                    <label for="bn" class="col-sm-2 control-label">销售价</label>

                    <div class="col-sm-8">
                      <input type="text" id="price" class="form-control" style="width: 120px;" name="price[]" placeholder="销售价" value="<%=StringUtil.setPrice(goods.products[0].price)%>">
                      <%if(lvlist.length>0){
                        var lv_p=[];
                        lvpricelist.forEach(function(lv){
                          if(lv.productid== goods.products[0].id){
                            var lvo={};
                            lvo.lv_id= lv.lvid;
                            lvo.lv_price= StringUtil.setPrice(lv.price);
                            lv_p.push(lvo);
                          }
                        });
                      %>
                      <input type="hidden" name="lvprice[]" value="<%=JSON.stringify(lv_p)%>">
                      <button class="btn btn-primary" style="float: left;margin-top: 2px;" type="button" data-toggle="modal"
                              data-target="#dialogLv">编辑会员价格</button>
                      <%}else {%>
                      <input type="hidden" name="lvprice[]" value="[]">
                      <%}%>
                    </div>
                  </div>
                  <div class="form-group">
                    <label for="bn" class="col-sm-2 control-label">市场价</label>

                    <div class="col-sm-8">
                      <input type="text" id="priceMarket" class="form-control" style="width: 120px;" name="priceMarket[]" placeholder="市场价" value="<%=StringUtil.setPrice(goods.products[0].priceMarket)%>">

                    </div>
                  </div>
                  <div class="form-group" style="display: none">
                    <label for="priceCost" class="col-sm-2 control-label">成本价</label>

                    <div class="col-sm-8">
                      <input type="text" id="priceCost" class="form-control" style="width: 120px;" name="priceCost[]" placeholder="前台不显示">

                    </div>
                  </div>
                  <div class="form-group">
                    <label for="pbn" class="col-sm-2 control-label">货号</label>

                    <div class="col-sm-8">
                      <input type="text" id="pbn" class="form-control" name="pbn[]" placeholder="货号(不填则自动生成)" value="<%=goods.products[0].gn%>">

                    </div>
                  </div>
                  <div class="form-group">
                    <label for="weight" class="col-sm-2 control-label">重量</label>

                    <div class="col-sm-8">
                      <input type="text" id="weight" class="form-control" style="width: 120px;" name="weight[]" placeholder="克(g)" value="<%=goods.products[0].weight%>">

                    </div>
                  </div>
                  <div class="form-group">
                    <label for="stock" class="col-sm-2 control-label">库存</label>

                    <div class="col-sm-8">
                      <input type="text" id="stock" class="form-control" style="width: 120px;" name="stock[]" placeholder="库存" value="<%=goods.products[0].stock%>">

                    </div>
                  </div>
                  <div class="form-group">
                    <label for="buyMin" class="col-sm-2 control-label">最小购买量</label>

                    <div class="col-sm-8">
                      <input type="text" id="buyMin" class="form-control" style="width: 120px;" value="<%=goods.products[0].buyMin%>" name="buyMin[]" placeholder="最小购买量">

                    </div>
                  </div>
                  <div class="form-group">
                    <label for="buyMax" class="col-sm-2 control-label">最大购买量</label>

                    <div class="col-sm-8">
                      <input type="text" id="buyMax" class="form-control" style="width: 120px;" value="<%=goods.products[0].buyMax%>" name="buyMax[]" placeholder="最大购买量">

                    </div>
                  </div>
                  <div class="form-group" id="gg" style="display: <%if(goods.speclist.length>0){%>block<%}else {%>none<%}%>">
                    <label for="gg" class="col-sm-2 control-label">规格</label>

                    <div class="col-sm-8">
                      <button id="specBtn" class="btn btn-primary" style="float: left;" type="button">开启规格</button>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <label for="unit" class="col-sm-2 control-label">计量单位</label>

                  <div class="col-sm-8">
                    <input type="text" class="form-control" style="width: 120px;" name="unit" placeholder="计量单位" value="<%=goods.unit||''%>">

                  </div>
                </div>
                <div class="form-group">
                  <label for="unit" class="col-sm-2 control-label">是否上架</label>

                  <div class="col-sm-8" style="float: left;text-align: left">
                    <input type="radio" name="disabled" value="0" <%=goods.disabled==false?'checked':''%>>上架
                    <input type="radio" name="disabled" value="1" <%=goods.disabled==true?'checked':''%>>下架

                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="tab-pane fade" id="kzsx">
              <%goods.propslist.forEach(function(prop){
              if(prop.type=='select') {
                %>
            <div class="form-group">
              <label class="col-sm-2 control-label"><%=prop.name%></label>
              <div class="col-sm-8">
                <select class="form-control" >
                  <option value=""></option>
                  <%prop.values.forEach(function(value){
                    %>
                  <option value="<%=value.name%>" <%goods.prop.forEach(function(p){if(p.name==prop.name&&value.name==p.value){%>selected<%}});%>><%=value.name%></option>
                  <%
                  });%>
                </select>
              </div>
            </div>
               <%
              }else{
%>
            <div class="form-group">
              <label class="col-sm-2 control-label"><%=prop.name%></label>
              <div class="col-sm-8">
                <input class="form-control" value="<%goods.prop.forEach(function(p){if(p.name==prop.name){%><%=p.value%><%}});%>"/>
              </div>
            </div>
            <%

              }%>

              <%});%>
          </div>
          <div class="tab-pane fade" id="inf">
            <div class="row mb10">
              <div class="col-lg-12" style="text-align:left">
                <textarea id="note" name="note" style="width:100%;height:200px;"><%=goods.note||''%></textarea>
              </div>
            </div>
          </div>
          <div class="tab-pane fade" id="seo">

            <div class="form-group">
              <label for="title" class="col-sm-2 control-label">网页标题</label>

              <div class="col-sm-8">
                <input type="text" id="title" class="form-control" name="title"
                       placeholder="title"  value="">
              </div>
            </div>
            <div class="form-group">
              <label for="keywords" class="col-sm-2 control-label">网页关键词</label>

              <div class="col-sm-8">
                <input type="text" id="keywords" class="form-control" name="keywords"
                       placeholder="keywords"  value="">
              </div>
            </div>
            <div class="form-group">
              <label for="description" class="col-sm-2 control-label">网页描述</label>

              <div class="col-sm-8">
                <input type="text" id="description" class="form-control" name="description"
                       placeholder="description"  value="">
              </div>
            </div>

          </div>
        </div>
      </div>
      <div class="col-lg-3"></div>
      <input id="is_spec" name="is_spec" type="hidden" value="<%=is_spec%>">
      <input id="specs" name="specs" type="hidden" value="">
      <input id="images" name="images" type="hidden" value="">
      <input id="spec_values" name="spec_values" type="hidden" value="[]">
      <input id="prop_values" name="prop_values" type="hidden" value="[]">
    </form>

  </div>
</div>
<!-- 选择上级 -->
<div id="dialogSelectParentUnit" class="modal fade bs-modal-sm" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h4 class="modal-title">选择分类</h4>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-xs-12">
            <div id="jsTreeParentUnit" class="demo"></div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <div class="pull-left">
          <button type="button" class="btn btn-success" data-dismiss="modal" onclick="selectFirstMenu()">不限分类</button>
        </div>
        <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
        <button type="button" class="btn btn-primary" onclick="selectParentMenu()">确认选择</button>
      </div>
    </div>
  </div>
</div>
<div id="dialogLv" class="modal fade bs-modal-sm" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h4 class="modal-title">编辑会员价</h4>
      </div>
      <div class="modal-body">

        <%lvlist.forEach(function(o){

          var lv_p_p='';
          lvpricelist.forEach(function(lv){
            if(lv.productid== goods.products[0].id&&lv.lvid==o.id){
              lv_p_p= StringUtil.setPrice(lv.price);
            }
          });
        %>

        <div class="row mb10 lvpricecss"  data-lv-id="<%=o.id%>">
          <div class="form-group">
            <label for="bn" class="col-sm-3 control-label" style="padding-top: 10px;"><%=o.name%></label>
            <div class="col-sm-4">
              <input type="text" class="form-control" style="width: 120px;" placeholder="" value="<%=lv_p_p%>">

            </div>
            <div class="col-sm-3" style="padding-top: 10px;">
              折扣率:<%=o.dis_count%>%
            </div>
          </div>
        </div>
        <%});%>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" onclick="lvpriceSet()">确认</button>
      </div>
    </div>
  </div>
</div>
<div id="specHtml">
  <%if(is_spec){%>
  <div id="dialogSpec" class="modal fade" tabindex="-2" role="dialog" aria-hidden="true" style="display: none;">
    <div class="modal-dialog" style="width: 1000px;">
      <div class="modal-content" style="width: 1000px;">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h4 class="modal-title">商品规格</h4>
        </div>
        <div class="modal-body" style="min-height: 300px;">
          <div class="row">
            <div class="col-xs-12" style="float: left">
              <% goods.speclist.forEach(function(o){ %>
              <div class="spec" style="float: left;padding-right: 10px;" data-spec-name="<%= o.name%>" data-spec-type="<%= o.type%>" data-spec-id="<%= o.id||'' %>">
                <span class="h5 mb10" style="float: left"><input class="specChkAll" type="checkbox"><strong><%= o.name||'' %>:</strong></span>
                <div style="float: left;padding-left: 20px;">
                  <% o.values.forEach(function(v){ %>
                  <div class="vl" >
                    <input id="s<%= v.id %>" type="checkbox" value="<%= v.id %>"
                           title="<%= v.spec_value||'' %>"><%if(v.spec_picurl){%><img src="<%=v.spec_picurl||''%>" style="width: 12px;height: 12px;"><%}%><%= v.spec_value||'' %>

                    <a href="javascript:selImg('<%= v.id %>');" style="color: #428bca">选择图片</a>
                    <div id="s_img<%= v.id %>">
                      <%goods.spec.forEach(function(ss){
                      ss.forEach(function(s){
                      if(s.spec_value_id== v.id&& s.spec_id== o.id&& s.spec_value_imgurl){
                      %>
                      <img class="specImg" width="50" height="50" src="<%=s.spec_value_imgurl%>?type=s" style="height: 50px;width: 50px;">
                      <%}
                      if(s.spec_value_id== v.id&& s.spec_id== o.id){%>
                      <script language="JavaScript">
                        $(function(){
                          $("#s<%=s.spec_value_id%>").prop('checked',true);
                        });
                      </script>
                      <%}});});%>
                    </div>
                  </div>
                  <% }); %>
                </div>
              </div>
              <% }); %>
            </div>
            <div class="col-xs-12" style="padding-top: 5px;height: 45px;">
              <button id="doProduct" type="button" class="btn btn-primary btn-sm">生成所有货品</button>
            </div>
            <div style="width: 100%;text-align: center;margin-top: 10px;">
              <table id="plist" class="table table-bordered table-striped table-condensed" style="width: 95%;margin: 20px;">
                <thead>
                <tr>
                  <th>规格值</th>
                  <th>货号</th>
                  <th class="numeric">上架</th>
                  <th class="numeric">库存</th>
                  <th class="numeric">最小购买</th>
                  <th class="numeric">最大购买</th>
                  <th class="numeric">销售价</th>
                  <th class="numeric" style="display: none">成本价</th>
                  <th class="numeric">市场价</th>
                  <th class="numeric">重量(g)</th>
                  <th class="numeric">默认货品</th>
                  <th class="numeric">操作</th>
                </tr>
                </thead>
                <tbody>
                <%goods.products.forEach(function(p){
                var lvp=[];
                lvpricelist.forEach(function(lv){
                  if(lv.productid== p.id){
                    var lvo={};
                    lvo.lv_id= lv.lvid;
                    lvo.lv_price= StringUtil.setPrice(lv.price);
                    lvp.push(lvo);
                  }
                });
                %>
                <tr><td><%=p.spec%></td>
                  <td><input name="pbn[]" type="text" class="form-control" value="<%=p.gn%>" style="height: 22px;width: 80px;padding: 0 2px;"></td>
                  <td><input name="up[]" type="checkbox" <%=p.disabled==true?'':'checked'%>></td>
                  <td><input name="stock[]" type="text" value="<%=p.stock%>" class="form-control" style="height: 22px;width: 60px;padding: 0 2px;"></td>
                  <td><input name="buyMin[]" type="text" value="<%=p.buyMin%>" class="form-control" style="height: 22px;width: 60px;padding: 0 2px;"></td>
                  <td><input name="buyMax[]" type="text" value="<%=p.buyMax%>" class="form-control" style="height: 22px;width: 60px;padding: 0 2px;"></td>
                  <td style="text-align: left;width: 150px;">
                    <div style="float: left"><input name="price[]" type="text" value="<%=StringUtil.setPrice(p.price)%>" class="form-control" style="height: 22px;width: 60px;padding: 0 2px;"></div>
                    <div style="float: left"><input name="lvprice[]" type="hidden" value="<%=JSON.stringify(lvp)%>">
                      <a class="member" href="javascript:;" style="color: #428bca">会员价</a></div></td>
                  <td><input name="priceMarket[]" value="<%=StringUtil.setPrice(p.priceMarket)%>"  type="text" class="form-control" style="height: 22px;width: 60px;padding: 0 2px;"></td>
                  <td style="display: none"><input name="priceCost[]" value="" type="text" class="form-control" style="height: 22px;width: 60px;padding: 0 2px;"></td>
                  <td><input name="weight[]" value="<%=p.weight%>"  type="text" class="form-control" style="height: 22px;width: 60px;padding: 0 2px;"></td>
                  <td><input name="is_default[]" type="checkbox" <%=p.is_default==true?'checked':''%>></td>
                  <td><a class="delSpec" href="javascript:;" style="color: #428bca">删除</a></td></tr>
               <%});%>
                </table>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
          <button id="dialogSpecOK" type="button" class="btn btn-primary" data-loading-text="保存...">保 存</button>
        </div>
      </div>
    </div>
  </div>
  <div id="dialogImg" class="modal fade" style="padding-top: 20px; display: none;" tabindex="-2" role="dialog" aria-hidden="true">
    <div class="modal-dialog" style="width: 650px;">
      <div class="modal-content" style="width: 650px;">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h4 class="modal-title">选择商品图片</h4>
        </div>
        <div class="modal-body">
          <div class="row" id="dialogImg_imgs"><div style="margin:2px;float:left;text-align: center;border: 1px solid #dcdcdc;cursor: pointer;" onclick="specSelImg(12,'/open/image/file/2a613b2d5f1e4020b960909e58ecf4cf.jpg?type=s')"><img src="/open/image/file/2a613b2d5f1e4020b960909e58ecf4cf.jpg?type=s" style="width:100px;height: 80px;margin-bottom: 1px;"></div><div style="margin:2px;float:left;text-align: center;border: 1px solid #dcdcdc;cursor: pointer;" onclick="specSelImg(12,'/open/image/file/b6ad2f5ea493403dbe7727ff4c6ece0a.jpg?type=s')"><img src="/open/image/file/b6ad2f5ea493403dbe7727ff4c6ece0a.jpg?type=s" style="width:100px;height: 80px;margin-bottom: 1px;"></div></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
          <button id="specImgDelBtn" type="button" class="btn btn-danger" data-loading-text="删除...">删 除</button>

        </div>
      </div>
    </div>
  </div>
  <div id="dialogLvSpec" style="padding-top: 20px; display: none;" class="modal fade bs-modal-sm" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h4 class="modal-title">编辑会员价</h4>
        </div>
        <div class="modal-body">



          <div class="row mb10 lvpricecss" data-lv-id="1">
            <div class="form-group">
              <label for="bn" class="col-sm-3 control-label" style="padding-top: 10px;">注册会员</label>
              <div class="col-sm-4">
                <input type="text" class="form-control" style="width: 120px;" placeholder="">

              </div>
              <div class="col-sm-3" style="padding-top: 10px;">
                默认折扣率:100%
              </div>
            </div>
          </div>


          <div class="row mb10 lvpricecss" data-lv-id="2">
            <div class="form-group">
              <label for="bn" class="col-sm-3 control-label" style="padding-top: 10px;">一级会员</label>
              <div class="col-sm-4">
                <input type="text" class="form-control" style="width: 120px;" placeholder="">

              </div>
              <div class="col-sm-3" style="padding-top: 10px;">
                默认折扣率:90%
              </div>
            </div>
          </div>


        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-primary" data-dismiss="modal">确认</button>
        </div>
      </div>
    </div>
  </div>
  <script language="JavaScript">
    function specSelImg(id,url){
      $("#s_img"+id).html("<img class='specImg' width='50' height='50' src='"+url+"' style='height: 50px;width: 50px;'>");
      $("#dialogImg").modal("hide");
    }
    function specDelImg(id){
      $("#s_img"+id).html("");
    }
    function selImg(id){
      var imgs=getImg();
      $("#dialogImg_imgs").html("");
      $.each(imgs,function(i,o){
        var str="<div style='margin:2px;float:left;text-align: center;border: 1px solid #dcdcdc;cursor: pointer;' onclick=\"specSelImg("+id+",'"+o.url+"')\" >" +
          "<img src='" + o.url + "' style='width:100px;height: 80px;margin-bottom: 1px;'></div>";
        $("#dialogImg_imgs").append(str);
        $("#specImgDelBtn").unbind("click").on("click",function(){
          specDelImg(id);
          $("#dialogImg").modal("hide");
        });
      });
      $("#dialogImg").modal("show");
    }
    function getSpec(){
      var specs=[];
      $("#dialogSpec").find("div .spec").each(function(){
        var self=$(this);
        var spec={};
        var spec_values=[];
        spec.spec_name=$(this).attr("data-spec-name");
        spec.spec_type=$(this).attr("data-spec-type");
        spec.spec_id=$(this).attr("data-spec-id");

        self.find("div .vl").each(function(){
          var v={};
          v.spec_name=spec.spec_name;
          v.spec_type=spec.spec_type;
          v.spec_id=spec.spec_id;
          var chk=false;
          $(this).find("img[class='specImg']").each(function(){
            v.spec_value_imgurl=$(this).attr("src");
          });
          $(this).find("input[type=checkbox]").each(function(){
            v.spec_value_id=$(this).val();
            v.spec_value_name=$(this).attr("title");
            chk=$(this).prop("checked");
          });
          if(chk){
            spec_values.push(v);
            spec.spec_values=spec_values;
          }
        });
        if(spec_values.length>0){
          specs.push(spec);
        }

      });
      return specs;
    }
    function getProducts(specs) {
      if (!specs || specs.length == 0) {
        return [];
      } else {
        return joinSpec([[]], specs, 0, specs.length-1);
      }
      function joinSpec(prevProducts, specs, i, max) {
        var currentProducts = [], currentProduct, currentSpecs = specs[i];
        if ( i > max ) {
          return prevProducts;
        }
        $.each(prevProducts,function(i,prevProduct) {
          $.each(currentSpecs,function(j,spec) {
            currentProduct = prevProduct.slice(0);
            currentProduct.push(spec);
            currentProducts.push(currentProduct);
          });
        });
        return joinSpec(currentProducts, specs, ++i, max);
      }
    }
    $(document).ready(function () {
      $(".specChkAll").on("click",function(){
        if($(this).prop("checked")){
          $(this).parent().parent().find("input[type=checkbox]").each(function(){
            $(this).prop("checked",true);
          });
        }else {
          $(this).parent().parent().find("input[type=checkbox]").each(function(){
            $(this).prop("checked",false);
          });
        }
      });
      $("#doProduct").on("click",function(){
        var specs=getSpec();
        var product_name=[];
        var spec_values=[];
        var size=1;
        $.each(specs,function(i,s){
          size=size * s.spec_values.length;
          var t=[];
          $.each(s.spec_values,function(j,o){
            t.push(o);
          });
          spec_values.push(t);
        });
//      console.log('spec_values::'+JSON.stringify(spec_values));
        $.each(getProducts(spec_values),function(i,v){
          var n="";
          $.each(v,function(j,s){
            n=n+ s.spec_name+":"+ s.spec_value_name;
            if(j< v.length-1)n+="*";
          });
          product_name.push(n);
        });
        if(spec_values.length<1){
          Toast.warning("至少选择一种规格");
          return false;
        }
        //console.log("product_name::"+JSON.stringify(product_name));
        $("#plist tbody").html("");
        var i=0;
        var pbn=$("#pbn").val();
        var price=$("#price").val();
        var priceMarket=$("#priceMarket").val();
        var priceCost=$("#priceCost").val();
        var weight=$("#weight").val();
        $.each(product_name,function(i,name){
          i++;
          var str="<tr>";
          var bn="";
          if(pbn!=""){
            bn=pbn+"-"+i;
          }
          str+="<td>"+name+"</td>";
          str+="<td><input name='pbn[]' type='text' class='form-control'  value='"+bn+"' style='height: 22px;width: 80px;padding: 0 2px;'></td>";
          str+="<td><input name='up[]' type='checkbox' checked></td>";
          str+="<td><input name='stock[]' type='text' class='form-control' style='height: 22px;width: 60px;padding: 0 2px;'></td>";
          str+="<td><input name='buyMin[]' type='text' class='form-control' style='height: 22px;width: 60px;padding: 0 2px;'></td>";
          str+="<td><input name='buyMax[]' type='text' class='form-control' style='height: 22px;width: 60px;padding: 0 2px;'></td>";
          str+="<td style='text-align: left;width: 150px;'><div style='float: left'><input name='price[]' type='text' value='"+price+"' class='form-control' style='height: 22px;width: 60px;padding: 0 2px;'></div><div style='float: left'>" +
            "<input name='lvprice[]' type='hidden' value='[]'><a class='member' href='javascript:;' style='color: #428bca'>会员价</a></div></td>";
          str+="<td><input name='priceMarket[]' value='"+priceMarket+"' type='text' class='form-control' style='height: 22px;width: 60px;padding: 0 2px;'></td>";
          str+="<td style='display: none'><input name='priceCost[]' value='"+priceCost+"' type='text' class='form-control' style='height: 22px;width: 60px;padding: 0 2px;'></td>";
          str+="<td><input name='weight[]' value='"+weight+"' type='text' class='form-control' style='height: 22px;width: 60px;padding: 0 2px;'></td>";
          str+="<td><input name='is_default[]' type='checkbox'></td>";
          str+="<td><a class='delSpec' href='javascript:;' style='color: #428bca'>删除</a></td>";
          $("#plist tbody").append(str);
        });
        $("#plist .delSpec").on("click",function(){
          $(this).parent().parent().remove();
        });
        $("#plist .member").on("click",function(){
          var self=$(this).parent().find("input[type=hidden]");
          var lvprice=self.val();
          if(lvprice!=""&&lvprice!="[]"){
            var lvp=JSON.parse(lvprice);
            $.each(lvp,function(i,lv){
              var j=0;
              $("#dialogLvSpec").find("input[type=text]").each(function(){
                if(i==j)$(this).val(lv.lv_price);
                j++;
              });
            });
          }else {
            $("#dialogLvSpec").find("input[type=text]").each(function(){
              $(this).val("");
            });
          }
          $("#dialogLvSpec button[type=button]").unbind("click").on("click",function(){
            var lv=[];
            $("#dialogLvSpec").find(".lvpricecss").each(function(){
              var id=$(this).attr("data-lv-id");
              var price=$(this).find("input[type=text]").val();
              if(price!="")
                lv.push({'lv_id':id,'lv_price':price});
            });
            self.val(JSON.stringify(lv));
          });
          $("#dialogLvSpec").modal("show");
        });
      });
      $("#dialogSpecOK").on("click",function(){
        var is_default=0;
        $("#plist tbody").find("input[name='is_default[]']").each(function(){
          if($(this).prop("checked"))is_default++;
        });
        if(is_default==1){
          var specs=getSpec();
          var spec_values=[];
          var size=1;
          $.each(specs,function(i,s){
            size=size * s.spec_values.length;
            var t=[];
            $.each(s.spec_values,function(j,o){
              t.push(o);
            });
            spec_values.push(t);
          });
          $("#spec_values").val(JSON.stringify(spec_values));
          var specs=[];
          var specs_name="";
          $("#plist tbody").find("tr").each(function(){
            var self=$(this);
            var spec=self.find("td:first").text();
            specs_name+=spec+"|";
            var gn=self.find("input[name='pbn[]']").val();
            var disabled=self.find("input[name='up[]']").prop("checked")==false;
            var stock=self.find("input[name='stock[]']").val();
            var buyMin=self.find("input[name='buyMin[]']").val();
            var buyMax=self.find("input[name='buyMax[]']").val();
            var price=self.find("input[name='price[]']").val();
            var lvprice=self.find("input[name='lvprice[]']").val();
            var priceMarket=self.find("input[name='priceMarket[]']").val();
            var priceCost=self.find("input[name='priceCost[]']").val();
            var weight=self.find("input[name='weight[]']").val();
            var is_default=self.find("input[name='is_default[]']").prop("checked")==true;
            specs.push({'spec':spec,'gn':gn,'disabled':disabled,'stock':stock,'buyMin':buyMin,'buyMax':buyMax,'price':price,
              'lvprice':JSON.parse(lvprice),'priceMarket':priceMarket,'priceCost':priceCost,'weight':weight,'is_default':is_default});
          });
          $("#is_spec").val("true");
          $("#specs").val(JSON.stringify(specs));
          $("#sp").hide();
          $("#sp_new").html('<div class="form-group" id="gg_new">'+
            '<label for="gg_new" class="col-sm-2 control-label">规格</label>'+
            '<div class="col-sm-8">'+
            '<button id="specEditBtn" class="btn btn-primary" style="float: left;" type="button">编辑规格</button>'+
            '<div style="float: left;text-align: left;">已设置'+specs.length+'个规格货品['+ specs_name+']</div>'+
            '</div>'+
            '</div>').show();
          $("#specEditBtn").unbind("click").on("click",function(){
            $("#dialogSpec").modal("show");
          });
          $("#dialogSpec").modal("hide");
        }else {
          Toast.warning("请选择一个货品为默认货品");
          return false;
        }
      });
    });
  </script>
<%}%>
</div>
<script language="JavaScript">
  var imgId=0;
  function initTreeView() {
    $("#jsTreeParentUnit").jstree({
      plugins: ["wholerow", "json_data"],
      core: {
        data: {
          dataType: "json",
          url: function (node) {
            return node.id === "#" ? "/private/shop/goods/class/tree" : "/private/shop/goods/class/tree?pid=" + node.id
          }
        },
        multiple: false
      }
    }).on("dblclick.jstree", function (node) {
      selectParentMenu();
    });
  }
  function getProps(id){
    $.post("/private/shop/goods/goods/getProps/" + id, {}, function (propdata) {
      if(propdata.code==0){
        var str='';
        $.each(propdata.data,function(i,prop){
          if(prop.type=='select'){
            str+='<div class="form-group">';
            str+='<label class="col-sm-2 control-label">'+prop.name+'</label>';
            str+='<div class="col-sm-8">';
            str+='<select class="form-control" >';
            str+='<option value=""></option>';
            $.each(prop.values,function(j,value){
              str+='<option value="'+value.name+'">'+value.name+'</option>';
            });
            str+='</select>';
            str+='</div>';
            str+='</div>';
          }else {
            str+='<div class="form-group">';
            str+='<label class="col-sm-2 control-label">'+prop.name+'</label>';
            str+='<div class="col-sm-8">';
            str+='<input class="form-control" />';
            str+='</div>';
            str+='</div>';
          }
        });
        $("#kzsx").html(str);
      }else {
        $("#kzsx").html('');
      }
    }, "json");
  }
  //选择父菜单
  function selectParentMenu() {
    var tree = $.jstree.reference("#jsTreeParentUnit");
    var node = tree.get_selected(true);
    var id=node[0].id;
    $("#addForm #classid").val(node[0].text);
    $("#addForm input[name='classid']").val(id);
    $("#dialogSelectParentUnit").modal("hide");
    $.post("/private/shop/goods/goods/getClass/" + id, {}, function (data) {
      if(data.code==0){
        var type=data.data.typeid;
        if(type){
          $("#typeid option[value='"+type.id+"']").attr("selected", true);
          if(type.use_props){
            $("#kzsx_tab").show();
            getProps(type.id);
          }else{
            $("#kzsx_tab").hide();
          }
          if(type.use_spec){
            $("#gg").show();
          }else{
            $("#gg").hide();
          }
        }
      }else{
        Toast.error(data.msg);
      }
    }, "json");
  }
  function selectFirstMenu() {
    $("#addForm #classid").val("请选择商品分类");
    $("#addForm input[name='classid']").val("");
    $("#dialogSelectParentUnit").modal("hide");
  }
  function setImg(id){
    $("#img").find("div").each(function(){
      $(this).removeClass("divImgD").addClass("divImg");
    });
    $("#"+id).removeClass("divImg");
    $("#"+id).addClass("divImgD");
  }
  function delImg(id){
    $("#"+id).remove();
  }
  function getImg(){
    var imgs=[];

    $("#img").find("div").each(function(){
      var img={};
      img.d=false;
      if($(this).hasClass("divImgD")){
        img.d=true;
      }
      $(this).find("img").each(function(){
        img.url=$(this).attr("src");
      });
      imgs.push(img);
    });
    return imgs;
  }
  function lvpriceSet(){
    var lv=[];
    var self=$("#sp").find("input[type=hidden]");
    $("#dialogLv").find(".lvpricecss").each(function(){
      var id=$(this).attr("data-lv-id");
      var price=$(this).find("input[type=text]").val();
      if(price!="")
        lv.push({'lv_id':id,'lv_price':price});
    });
    self.val(JSON.stringify(lv));
    $("#dialogLv").modal("hide");
  }
  $(document).ready(function () {

    $("#typeid").on("change",function(){
      if($(this).val()=='0'){
        $("#kzsx_tab").hide();
        $("#gg").hide();
        $("#kzsx").html('');
        $("#sp_new").html('').hide();
        $("#sp").show();
        $("#is_spec").val("false");
        $("#specs").val('[]');
        $("#spec_values").val('[]');
        $("#prop_values").val('[]');

      }else {
        $.post("/private/shop/goods/goods/getType/" + $(this).val(), {}, function (data) {

          if(data.code==0){
            $("#sp_new").html('').hide();
            $("#sp").show();
            $("#is_spec").val("false");
            $("#specs").val('[]');
            $("#spec_values").val('[]');
            $("#prop_values").val('[]');
            if(data.data.use_props){
              $("#kzsx_tab").show();
              getProps(data.data.id);
            }else{
              $("#kzsx_tab").hide();
            }
            if(data.data.use_spec){
              $("#gg").show();
            }else{
              $("#gg").hide();
            }
          }else{
            Toast.error(data.msg);
          }
        }, "json");
      }
    });
    $("#specBtn").on("click",function(){
      if($("#pbn").val()==""){
        Toast.warning("请填写货号");
        $("#pbn").focus();
        return false;
      }
      $("#specHtml").load("/private/shop/goods/goods/spec/"+$("#typeid").val(), function (response, status, xhr) {
        $("#dialogSpec").modal("show");
      });
    });
    <%if(is_spec){%>
    //start spec
    $("#specEditBtn").unbind("click").on("click",function(){
      $("#dialogSpec").modal("show");
    });
    $("#dialogSpecOK").trigger("click");
    $("#plist .delSpec").on("click",function(){
      $(this).parent().parent().remove();
    });
    $("#plist .member").on("click",function(){
      var self=$(this).parent().find("input[type=hidden]");
      var lvprice=self.val();
      if(lvprice!=""&&lvprice!="[]"){
        var lvp=JSON.parse(lvprice);
        $.each(lvp,function(i,lv){
          var j=0;
          $("#dialogLvSpec").find("input[type=text]").each(function(){
            if(i==j)$(this).val(lv.lv_price);
            j++;
          });
        });
      }else {
        $("#dialogLvSpec").find("input[type=text]").each(function(){
          $(this).val("");
        });
      }
      $("#dialogLvSpec button[type=button]").unbind("click").on("click",function(){
        var lv=[];
        $("#dialogLvSpec").find(".lvpricecss").each(function(){
          var id=$(this).attr("data-lv-id");
          var price=$(this).find("input[type=text]").val();
          if(price!="")
            lv.push({'lv_id':id,'lv_price':price});
        });
        self.val(JSON.stringify(lv));
      });
      $("#dialogLvSpec").modal("show");
    });
  //end spec
    <%}%>
    initTreeView();
    var container = document.getElementById("img");
    var sort = Sortable.create(container);
    var ue = new baidu.editor.ui.Editor();
    ue.render('note');
    $('#file_upload').uploadifive({
      'auto': true,
      'multi': true,
      'width': '100%',
      'height': '35',
      'buttonText': '请选择图片',
      'fileType': 'image/*',
      'fileSizeLimit': 1024,
      'queueSizeLimit': 6,
      'formData': {},
      'queueID': 'queue',
      'uploadScript': '/open/fileupload/dbimage',
      'onUploadComplete': function (file, data) {
        data = JSON.parse(data);
        if (data.code == 0) {
          Toast.success(data.msg);
          imgId++;
          var c="divImg";
          if(imgId==1){
            c="divImgD";
          }
          $("#img").append("<div id='imgId"+imgId+"' class='"+c+"'>" +
            "<img  onclick=\"setImg('imgId"+imgId+"')\" src='" + data.path + "?type=s' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
            "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('imgId"+imgId+"')\"></i></div>");
          sort.destroy();
          sort = Sortable.create(container);
        } else {
          Toast.error(data.msg);
        }
      },
      'onSelect' : function(queue) {
        if($("#img").children().length>5){
          Toast.warning("最大允许上传6张图片");
          $('#file_upload').uploadifive('cancel', $('.uploadifive-queue-item').first().data('file'));
        }
      }
    });
    $("#save").on("click",function(){
      if ($("#is_spec").val() == "false") {
        var specs = [];
        var self = $("#sp");
        var spec = "";
        var gn = self.find("input[name='pbn[]']").val();
        var disabled = false;
        var stock = self.find("input[name='stock[]']").val();
        var buyMin = self.find("input[name='buyMin[]']").val();
        var buyMax = self.find("input[name='buyMax[]']").val();
        var price = self.find("input[name='price[]']").val();
        var lvprice = self.find("input[name='lvprice[]']").val();
        var priceMarket = self.find("input[name='priceMarket[]']").val();
        var priceCost = self.find("input[name='priceCost[]']").val();
        var weight = self.find("input[name='weight[]']").val();
        var is_default = true;
        specs.push({
          'spec': spec,
          'gn': gn,
          'disabled': disabled,
          'stock': stock,
          'buyMin': buyMin,
          'buyMax': buyMax,
          'price': price,
          'lvprice': JSON.parse(lvprice),
          'priceMarket': priceMarket,
          'priceCost': priceCost,
          'weight': weight,
          'is_default': is_default
        });
        $("#specs").val(JSON.stringify(specs));
      }
      var props=[];
      $("#kzsx").find(".form-group").each(function(){
        var self=$(this);
        var obj={};
        obj.name=self.find("label").text();
        obj.value=self.find("select").val()||self.find("input").val()||'';
        props.push(obj);
      });
      //console.log('props::'+JSON.stringify(props));
      $("#images").val(JSON.stringify(getImg()));
      $("#prop_values").val(JSON.stringify(props));
      $('#addForm').submit();
    });
    $('#addForm').ajaxForm({
      dataType: 'json',
      beforeSubmit: function (arr, form, options) {
        form.find("button:submit").button("loading");
      },
      success: function (data, statusText, xhr, form) {
        if (data.code == 0) {
          Toast.success(data.msg);
        } else {
          Toast.error(data.msg);
        }
        form.find("button:submit").button("reset");
      }
    });
  });
</script>
